var mobId, mob, stats, modified, MaxRandom, setupTask, ostats, victor, luck;
var 清怪 = true;
var 刷新地图 = false;
var 复活次数 = 11;
var 副本名称 = "戴米安[普通]";
var BOSS_1_id = 8880110;
var BOSS_1_血量倍率 = 1;
var BOSS_2_id = 8880111;
var BOSS_2_血量倍率 = 1;
var 副本时限 = 1000 * 60 * 20;
var 实例地图 = [350160260, 350160200, 350160220, 350160240];
var 准备地图 = 105300303;
var 退场倒计时 = 180;
var 动画 = true;

function init() {}

function setup(h, c) {
    em.setProperty("state", "1");
    em.setProperty("leader", "true");
    var e = em.newInstance(副本名称);
    for (var d = 0; d < 实例地图.length; d++) {
        var g = e.setInstanceMap(实例地图[d]);
        g.resetPQ(h);
        g.resetFully();
        g.killAllMonsters(清怪);
        g.respawn(刷新地图)
    }
    e.startEventTimer(副本时限);
    e.setProperty("FBtime", 副本时限);
    e.setProperty("FBname", 副本名称);
    e.setProperty("strMap", 准备地图);
    var f = em.getMonster(BOSS_1_id).getMobMaxHp() * BOSS_1_血量倍率;
    var b = em.getMonster(BOSS_2_id).getMobMaxHp() * BOSS_2_血量倍率;
    e.setProperty("BOSShp", f + b);
    mobId = BOSS_1_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = f;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    e.registerMonster(mob);
    var a = e.getMapInstance(实例地图[1]);
    a.spawnMonsterOnGroundBelow(mob, new java.awt.Point(1058, 16));
    mobId = BOSS_2_id;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = b;
    mob.getChangedStats().mp = 99000000;
    mob.heal();
    e.registerMonster(mob);
    var a = e.getMapInstance(实例地图[3]);
    a.spawnMonsterOnGroundBelow(mob, new java.awt.Point(1058, 18));
    return e
}

function onPlayerRegistered(b, a) {
    var c = b.getMapInstance(实例地图[动画 ? 0 : 1]);
    a.dropMessage(2, "[" + 副本名称 + "] 进入到了挑战地图，请小心行事。");
    a.changeMap(c, c.getPortal(0));
    a.setReviveCount(复活次数);
    a.getAPI().setNumberForQuestInfo(准备地图, "damage", 0)
}

function onPlayerRevived(b, a) {
    if (a.getReviveCount() > 0) {
        return true
    } else {
        var c = b.getMapFactoryMap(准备地图);
        a.changeMap(c, c.getPortal(0));
        return true
    }
}

function onMapChanged(c, b, a) {
    if (实例地图.indexOf(a) < 0) {
        on玩家退场(c, b, false);
        return
    }
    switch (a) {
        case 350160200:
            b.getAPI().戴米安墓碑();
            b.getAPI().戴米安飞箭(30);
            initProp("stage" + 1 + "_kill", 0);
            break;
        case 350160460:
            b.getAPI().戴米安墓碑();
            b.getAPI().戴米安飞箭(30);
            initProp("stage" + 1 + "_kill", 0);
            break
    }
}

function onMonsterKilled(b, c) {
    if (c == BOSS_1_id) {
        b.getPlayers().forEach(function (e) {
            e.scheduleWarpTask(3, 实例地图[动画 ? 2 : 3], 0, false)
        })
    } else {
        if (c == BOSS_2_id) {
            var d = b.getPlayerCount();
            b.getPlayers().forEach(function (e) {
                e.getAPI().updateAchievement(819, 0, 1, 1);
                e.getAPI().updateAchievement(820, 0, 1, 10);
                e.getAPI().updateAchievement(821, 0, 1, 100);
                e.getAPI().updateAchievement(822, 0, 1, 100)
            });
            if (d == 4) {
                player.getAPI().gainSubAchievement(823, 0)
            } else {
                if (d == 5) {
                    player.getAPI().gainSubAchievement(823, 1)
                } else {
                    if (d == 6) {
                        player.getAPI().gainSubAchievement(823, 2)
                    }
                }
            }
            if (副本时限 - b.getTimeLeft() < 5 * 60 * 1000) {
                player.getAPI().finishAchievement(824)
            }
            openNpc(b, 9130027, "评价系统");
            b.startEventTimer(退场倒计时 * 1000);
            for (var a = 0; a < b.getPlayerCount(); a++) {
                b.getPlayers().get(a).dropMessage(6, "[" + 副本名称 + "] 挑战成功！已召唤击杀奖励！！")
            }
        }
    }
    return 1
}

function onMonsterDamaged(b, a, d, e, c) {
    if (d == BOSS_1_id || d == BOSS_2_id) {
        a.getAPI().addNumberForQuestInfo(准备地图, "damage", e)
    }
}

function openNpc(c, b, d) {
    for (var a = 0; a < c.getPlayerCount(); a++) {
        c.getPlayers().get(a).getAPI().openNpc(b, d)
    }
}

function randomNum(b, a) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * b + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (a - b + 1) + b, 10);
            break;
        default:
            return 0;
            break
    }
}

function initProp(a, b) {
    if (em.getProperty(a) == null) {
        em.setProperty(a, b)
    }
}

function initPropArray(a, d, c, e) {
    for (var b = c; b <= e; b++) {
        if (em.getProperty(a + "_" + b) == null) {
            em.setProperty(a + "_" + b, d)
        }
    }
}

function initPropArrayRandom(a, d, j, b, h) {
    var g = new Array();
    var k = h - b + 1;
    var c = (j - d) < (h - b) ? true : false;
    do {
        var f = randomNum(d, j);
        if (c || g.indexOf(f) < 0) {
            g.push(f);
            k--
        }
    } while (k);
    for (var e = b; e <= h; e++) {
        if (em.getProperty(a + "_" + e) == null) {
            em.setProperty(a + "_" + e, g[e - 1])
        }
    }
}

function on玩家退场(c, b, a) {
    c.disposeIfPlayerBelow(a ? 999 : 0, 准备地图);
    em.getProperties().clear();
    em.setProperty("state", 0);
    em.setProperty("leader", "true");
    c.unregisterPlayer(b)
}

function onPlayerKilled(b, a) {}

function onPartyDisbanded(a) {
    on玩家退场(a, player, false)
}

function onTimeOut(a) {
    a.getPlayers().forEach(function (b) {
        on玩家退场(a, b, true)
    })
}

function onPlayerDisconnected(b, a) {
    on玩家退场(b, a, false);
    return 0
}

function onItemPickedUp(b, a, c) {};